let withDrawals = []
let ajax = getApp().globalData.ajax;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    withDrawals,
    start: 0,
    max: 5,
    balance: 0,
    money: 0,
    withDrawalFlag: false,
    errorTip: '',
    balanceFlag: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      money: options.balance
    })
    let json = {
      token: wx.getStorageSync('Cookie'),
      start: this.data.start,
      max: this.data.max,
    };
    this.getList(json)
  },

  getList (json) {
    ajax('user/myWithDrawOrders', json).then(res => {
      withDrawals.push(...res.data)
      withDrawals.forEach(item => {
        item.date = item.create_time.toDateString();
        switch (item.status) {
          case 'INIT':
            item.statusRes = '提现中';
            break;
          case 'FAIL':
            item.statusRes = '提现失败';
            break;
          case 'SUCCESS':
            item.statusRes = '提现成功';
            break;
        }

      })

      this.setData({ withDrawals })
    })
  },
  changeBalance (e) {
    let reg = /^\d+(\.\d+)?$/
    let value = parseInt(e.detail.value, 10)
    let balanceFlag = value != 0 && reg.test(value)
    this.setData({ balanceFlag: balanceFlag, withDrawalFlag: false })
    if (balanceFlag) {
      if (value % 10 != 0) {
        if (value < 100) {
          this.setData({ balanceFlag: false, errorTip: '提现金额必须大于100元' })
        } else {
          this.setData({ balanceFlag: false, errorTip: '提现金额必须是10的倍数' })
        }
      } else {
        this.setData({ balance: value })
      }
    } else {
      this.setData({ errorTip: '请输入正确的金额' })
    }
  },
  withDrawal () {
    this.setData({ withDrawalFlag: false })
    if (!this.data.balanceFlag) {
      this.setData({ withDrawalFlag: true })
      return false
    }
    let json = {
      token: wx.getStorageSync('Cookie'),
      price: this.data.balance,
      order_type: 'WithDrawOrder',
    }
    ajax('/pay/add', json).then(res => {
      if (res.code === 0) {
        let order_no = res.data.order_no;
        let json = {
          order_no,
          token: wx.getStorageSync('Cookie'),
        }
        ajax('/pay/withDraw', json).then(res => {
          if (res.code === 0) {
            wx.showToast({
              title: '提现成功，注意查看',
              icon: 'success',
              image: '',
              duration: 1000,
              mask: true,
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          } else {
            if (res.code === -3) {
              wx.showToast({
                title: '金额不足，提现失败',
                icon: 'none',
                duration: 1500,
                mask: true
              })
            } else {
              wx.showToast({
                title: '提现失败',
                icon: 'none',
                duration: 1000,
                mask: true
              })
            }
          }
        })
      } else {
        if (res.code === -3) {
          wx.showToast({
            title: '金额不足，提现失败',
            icon: 'none',
            duration: 1500,
            mask: true
          })
        } else {
          wx.showToast({
            title: '提现失败',
            icon: 'none',
            duration: 1000,
            mask: true
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let start = this.data.start;
    start += this.data.max;
    this.setData({
      start,
    })
    let json = {
      token: wx.getStorageSync('Cookie'),
      start: this.data.start,
      max: this.data.max,
    }
    this.getList(json)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})